package com.galeno.utils;

import com.galeno.pojo.CombineCondition;
import com.galeno.pojo.EventCondition;
import com.galeno.pojo.EventParam;
import com.galeno.pojo.RuleCondition;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/11/1123:11
 */
public class RuleComparator {
    //获取所有的规则（以后可以从广播状态中获取）
    //运营人员事先配置好的营销规则（参数名和参数值）
    public static RuleCondition getRule() {
        RuleCondition ruleCondition = new RuleCondition();
        ruleCondition.setRuleId("rule000010");

        //事先定义好的触发行为规则
        String eventId = "A";
        HashMap<String, String> properties = new HashMap<>();
        properties.put("p2", "v1");
        properties.put("p20", "v2");
        EventParam eventParam = new EventParam(eventId, properties);
        ruleCondition.setTriggerEvent(eventParam);

        //事先定义好的用户画像规则
        HashMap<String, String> profileCondition = new HashMap<>();
        profileCondition.put("tag2", "v1");
        profileCondition.put("tag20", "v2");
        ruleCondition.setProfileCondition(profileCondition);

        //实现定义号的历史行为规则
        ArrayList<CombineCondition> combineConditions = new ArrayList<>();

        //查询历史行为次数的规则  B事件在指定时间内做过两次或以上
        CombineCondition combineCondition1 = new CombineCondition();
        ArrayList<EventCondition> eventConditions1 = new ArrayList<>();
        EventCondition eventCondition = new EventCondition();
        eventCondition.setEventId("B");
        combineCondition1.setEventConditions(eventConditions1);
        combineCondition1.setSql("select");
        combineCondition1.setStartTime(0L);
        combineCondition1.setEndTime(1L);
        combineCondition1.setRegex("(1)");
        combineCondition1.setMinLimit(2);
        combineCondition1.setMaxLimit(Integer.MAX_VALUE);

        //查询历史行为序列
        //规则2历史行为序列ABD在指定时间范围内出现过一次或者以上
        CombineCondition combineCondition2 = new CombineCondition();
        ArrayList<EventCondition> eventConditions2 = new ArrayList<>();
        EventCondition eventCondition2 = new EventCondition();
        //行为序列ABD
        eventCondition2.setEventId("A");
        EventCondition eventCondition3 = new EventCondition();
        eventCondition3.setEventId("B");
        EventCondition eventCondition4 = new EventCondition();
        eventCondition4.setEventId("D");
        eventConditions2.addAll(Arrays.asList(eventCondition2, eventCondition3, eventCondition4));
        combineCondition2.setSql("select eventId from eagle_detail where deviceId = ? and timeStamp >= ? and timeStamp <= ? order by timeStamp asc");
        combineCondition2.setStartTime(1636387200000L);
        combineCondition2.setEndTime(1636895339000L);
        combineCondition2.setRegex("(.*?1.*?2.*?3)");
        combineCondition2.setMinLimit(1);
        combineCondition2.setMaxLimit(Integer.MAX_VALUE);
        combineCondition2.setEventConditions(eventConditions2);
        combineConditions.add(combineCondition1);
        combineConditions.add(combineCondition2);
        ruleCondition.setCombineConditions(combineConditions);
        return ruleCondition;
//        //事先定义好的用户历史行为次数规则
//        ArrayList<EventParam> eventCountConditions = new ArrayList<>();
//        EventParam countCondition = new EventParam();
//        countCondition.setEventId("E");
//        HashMap<String, String> countConditionProps = new HashMap<>();
//        countConditionProps.put("p1", "v2");
//        countConditionProps.put("p7", "v1");
//        countCondition.setProperties(countConditionProps);
//        countCondition.setStartTime(1636473600000L);
//        countCondition.setEndTime(1636646399000L);
//        countCondition.setThreshold(2);
//        countCondition.setSql("select count(*) counts from eagle_detail where deviceId = ? and timeStamp >= ? and timeStamp <= ? and eventId = ? ");
//        eventCountConditions.add(countCondition);
//        ruleCondition.setEventCountCondition(eventCountConditions);
//
//
//        //添加一个行为序列规则
//        // P -> L - W  行为序列规则
//        // 时间范围，起始时间，结束时间
//        // 动态传入sql SQL
//        ArrayList<EventParam> eventSequence = new ArrayList<>();
//        EventParam eventParam1 = new EventParam();
//
//        String sql = "select\n" +
//                "  deviceId,\n" +
//                "  sequenceMatch('(?1)(?2)(?3)')\n" +
//                "  (\n" +
//                "    toDateTime(`timeStamp`),\n" +
//                "    eventId = 'P',\n" +
//                "    eventId = 'L',\n" +
//                "    eventId = 'W'\n" +
//                "  ) \n" +
//                "  as is_match\n" +
//                "from \n" +
//                "  eagle_detail\n" +
//                "where\n" +
//                "  deviceId = ? and timeStamp >= ? and timeStamp <= ?";
//        eventParam1.setSql(sql);
//        eventParam1.setStartTime(1636473600000L);
//        eventParam1.setEndTime(1636646399000L);
//        eventParam1.setThreshold(0);
//
//        eventParam1.setEventId("P");
//        HashMap<String, String> properties1 = new HashMap<String, String>();
//        properties1.put("p1", "v2");
//        eventParam1.setProperties(properties1);
//        EventParam eventParam2 = new EventParam();
//        eventParam2.setEventId("L");
//        EventParam eventParam3 = new EventParam();
//        eventParam3.setEventId("W");
//        HashMap<String, String> properties3 = new HashMap<String, String>();
//        properties3.put("p2", "v1");
//        properties3.put("p4", "v2");
//        eventParam3.setProperties(properties3);
//
//        // P {p1, v2}  ->   L   -> W
//        eventSequence.add(eventParam1);
//        eventSequence.add(eventParam2);
//        eventSequence.add(eventParam3);
//
//        ruleCondition.setEventSequenceCondition(eventSequence);
//


    }


}
